<?php
/*
Plugin Name: dKret now reading widget
Description: Adds a sidebar widget to display read books (needs now reading plugin)
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

if (!function_exists('have_books'))
	return; 

function widget_dkret_nowreading($args) {
	extract($args);
	$options = get_option('widget_dkret_nowreading');
	$title = empty($options['title']) ? __('Now Reading','dkret') : $options['title'];
	echo "\t\t\t$before_widget\n";
	echo "\t\t\t\t$before_title".$title.$after_title; ?>

<?php if( have_books('status=reading&num=1') ) : ?>

<?php while( have_books('status=reading') ) : the_book(); ?>
	<div class="book">
		<a href="<?php book_permalink() ?>" title="<?php book_author() ?>: <?php book_title() ?>"><img width="100" height="170" class="alignleft" src="<?php book_image() ?>" alt="<?php book_title() ?>" /></a>
		<a href="<?php book_permalink() ?>"><strong><?php book_title() ?></strong></a><br /><p><?php _e('by','dkret'); ?> <a href="<?php book_author_permalink() ?>"><?php book_author() ?></a></p>
	</div>
<?php endwhile; ?>
	
<?php endif; ?>

<?php if( have_books('status=unread&num=3') ) : ?>
	<h3><?php _e('Planned Books','dkret'); ?></h3>
	<div class="book">
<?php while( have_books('status=unread') ) : the_book(); ?>
		<a href="<?php book_permalink() ?>" title="<?php book_author() ?>: <?php book_title() ?>"><img class="alignleft" src="<?php book_image() ?>" width="65" height="105" alt="<?php book_title() ?>" /></a>
<?php endwhile; ?>
	</div>
<?php endif; ?>

<?php if( have_books('status=read&orderby=finished&order=desc&num=3') && $options['showrecent'] == 'checked="checked"' ) : ?>
	<h3><?php _e('Recent Books','dkret'); ?></h3>
	<div class="book">
<?php while( have_books('status=read&orderby=finished&order=desc') ) : the_book(); ?>
		<a href="<?php book_permalink() ?>" title="<?php book_author() ?>: <?php book_title() ?>"><img class="alignleft" src="<?php book_image() ?>" width="65" height="105" alt="<?php book_title() ?>" /></a>
<?php endwhile; ?>
	</div>
<?php endif;
	

	echo "\n\t\t\t$after_widget";
  }

// Admin section
function widget_control_dkret_nowreading() {
 	global $wpdb;
 	$options = get_option('widget_dkret_nowreading');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-nowreading-title']));
		$options['showrecent'] = isset($_POST['dkret-nowreading-recent']);
		update_option('widget_dkret_nowreading', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$showrecent = $options['showrecent'] ? 'checked="checked"' : '';
	
	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-nowreading-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-nowreading-title" name="dkret-nowreading-title" type="text" value="'.$title.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-nowreading-recent">' . __('Show Recent books:', 'dkret') . ' <input id="dkret-nowreading-recent" name="dkret-nowreading-recent" type="checkbox" '. $showrecent .'  /></label></p>';
	echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

$options = get_option('widget_dkret_nowreading');
if ( !is_array($options) )
	$options = array('showrecent'=>'checked="checked"',);
	update_option('widget_dkret_nowreading', $options);

$widget_ops = array('classname' => 'widget_dkret_nowreading', 'description' => __( "Your recent books (dkret)", 'dkret') );
wp_register_sidebar_widget('nowreading', __('Now Reading','dkret'), 'widget_dkret_nowreading', $widget_ops);
unregister_widget_control('Now Reading');
wp_register_widget_control('nowreading', __('Now Reading','dkret'), 'widget_control_dkret_nowreading');
?>
